﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Runtime.CompilerServices;
using HslCommunication.Core.Net;
using HslCommunication.LogNet;

namespace HslCommunication.Enthernet
{
	/// <summary>
	/// 用于服务器支持软件全自动更新升级的类<br />
	/// Class for server support software full automatic update and upgrade
	/// </summary>
	// Token: 0x02000100 RID: 256
	public sealed class NetSoftUpdateServer : NetworkServerBase
	{
		/// <summary>
		/// 实例化一个默认的对象<br />
		/// Instantiate a default object
		/// </summary>
		/// <param name="updateExeFileName">更新程序的名称</param>
		// Token: 0x06001561 RID: 5473 RVA: 0x0006EF64 File Offset: 0x0006D164
		public NetSoftUpdateServer(string updateExeFileName = "软件自动更新.exe")
		{
			this.updateExeFileName = updateExeFileName;
		}

		/// <summary>
		/// 系统升级时客户端所在的目录，默认为C:\HslCommunication
		/// </summary>
		// Token: 0x170004FF RID: 1279
		// (get) Token: 0x06001562 RID: 5474 RVA: 0x0006EF80 File Offset: 0x0006D180
		// (set) Token: 0x06001563 RID: 5475 RVA: 0x0006EF98 File Offset: 0x0006D198
		public string FileUpdatePath
		{
			get
			{
				return this.m_FilePath;
			}
			set
			{
				this.m_FilePath = value;
			}
		}

		/// <inheritdoc />
		// Token: 0x06001564 RID: 5476 RVA: 0x0006EFA4 File Offset: 0x0006D1A4
		[DebuggerStepThrough]
		protected override void ThreadPoolLogin(Socket socket, IPEndPoint endPoint)
		{
			NetSoftUpdateServer.<ThreadPoolLogin>d__6 <ThreadPoolLogin>d__ = new NetSoftUpdateServer.<ThreadPoolLogin>d__6();
			<ThreadPoolLogin>d__.<>t__builder = AsyncVoidMethodBuilder.Create();
			<ThreadPoolLogin>d__.<>4__this = this;
			<ThreadPoolLogin>d__.socket = socket;
			<ThreadPoolLogin>d__.endPoint = endPoint;
			<ThreadPoolLogin>d__.<>1__state = -1;
			<ThreadPoolLogin>d__.<>t__builder.Start<NetSoftUpdateServer.<ThreadPoolLogin>d__6>(ref <ThreadPoolLogin>d__);
		}

		// Token: 0x06001565 RID: 5477 RVA: 0x0006EFEC File Offset: 0x0006D1EC
		private void ReceiveCallBack(IAsyncResult ir)
		{
			Socket socket = ir.AsyncState as Socket;
			bool flag = socket != null;
			if (flag)
			{
				try
				{
					socket.EndReceive(ir);
				}
				catch (Exception ex)
				{
					ILogNet logNet = base.LogNet;
					if (logNet != null)
					{
						logNet.WriteException(this.ToString(), ex);
					}
				}
				finally
				{
					if (socket != null)
					{
						socket.Close();
					}
				}
			}
		}

		/// <summary>
		/// 获取所有的文件信息
		/// </summary>
		/// <param name="dircPath">目标路径</param>
		/// <param name="logNet">日志信息</param>
		/// <returns>文件名的列表</returns>
		// Token: 0x06001566 RID: 5478 RVA: 0x0006F068 File Offset: 0x0006D268
		public static List<string> GetAllFiles(string dircPath, ILogNet logNet)
		{
			List<string> list = new List<string>();
			try
			{
				list.AddRange(Directory.GetFiles(dircPath));
			}
			catch (Exception ex)
			{
				if (logNet != null)
				{
					logNet.WriteWarn("GetAllFiles", ex.Message);
				}
			}
			foreach (string dircPath2 in Directory.GetDirectories(dircPath))
			{
				list.AddRange(NetSoftUpdateServer.GetAllFiles(dircPath2, logNet));
			}
			return list;
		}

		/// <inheritdoc />
		// Token: 0x06001567 RID: 5479 RVA: 0x0006F0EC File Offset: 0x0006D2EC
		public override string ToString()
		{
			return string.Format("NetSoftUpdateServer[{0}]", base.Port);
		}

		// Token: 0x0400051C RID: 1308
		private string m_FilePath = "C:\\HslCommunication";

		// Token: 0x0400051D RID: 1309
		private string updateExeFileName;
	}
}
